using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
using LuckyFish.Ledger.App.ViewModels;
using LuckyFish.Ledger.Data;

namespace LuckyFish.Ledger.App.Views;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private async void AddClick(object? sender, RoutedEventArgs e)
    {
        var a = new AddLedgerView();
        var model = await a.ShowDialog<LedgerModel>(this);
        if(DataContext is not MainWindowViewModel data)return;
        if(model == null)return;
        data.Add(model);
    }

    private void ChooseSelectionChange(object? sender, SelectionChangedEventArgs e)
    {
        if((sender as ComboBox)?.SelectedItem is not string choose)return;
        
        if (choose == "From Name")
        {
            NameBox.IsVisible = true;
            CategoriesComboBox.IsVisible = false;
            LeastUpDown.IsVisible = false;
            UtmostUpDown.IsVisible = false;
            StartTimePicker.IsVisible = false;
            EndTimePicker.IsVisible = false;
            return;
        }
        
        if (choose == "From Amount")
        {
            NameBox.IsVisible = false;
            CategoriesComboBox.IsVisible = false;
            LeastUpDown.IsVisible = true;
            UtmostUpDown.IsVisible = true;
            StartTimePicker.IsVisible = false;
            EndTimePicker.IsVisible = false;
            return;
        }
        
        if (choose == "From Category")
        {
            NameBox.IsVisible = false;
            CategoriesComboBox.IsVisible = true;
            LeastUpDown.IsVisible = false;
            UtmostUpDown.IsVisible = false;
            StartTimePicker.IsVisible = false;
            EndTimePicker.IsVisible = false;
            return;
        }
        
        if (choose == "From CreateTime")
        {
            NameBox.IsVisible = false;
            CategoriesComboBox.IsVisible = false;
            LeastUpDown.IsVisible = false;
            UtmostUpDown.IsVisible = false;
            StartTimePicker.IsVisible = true;
            EndTimePicker.IsVisible = true;
            return;
        }
        
    }

    private async void AddCategoryClick(object? sender, RoutedEventArgs e)
    {
        var window = new AddCategoryView();
        var model = await window.ShowDialog<CategoryModel>(this);
        if(model == null)return;
        (DataContext as MainWindowViewModel)?.Add(model);
    }

    private void RemoveClick(object? sender, RoutedEventArgs e)
    {
        if(DataContext is not MainWindowViewModel model)return;
        model.Remove((sender as Control).DataContext as LedgerModel);
    }
}